<template>
  <div>
    <a-drawer
        :title="state === 'write' ?$t( 'user.verification-code.required1115661105772571') :$t( 'user.verification-code.required111566110577257')"
        :width="550" :visible.sync="visible"
        :body-style="{ 'padding-top': '0px', paddingBottom: '80px' }" @close="close">
      <a-form-item>
        <a-button type="primary" icon="plus" @click="create()">{{
            $t('user.verification-code.required1115661106')
          }}
        </a-button>
      </a-form-item>
      <!-- 主列表 -->
      <a-table ref="table" size="middle" class="h-full overflow-x-auto" :dataSource="listData"
               :pagination="true" @change="tableChange" :loading="listLoading" :rowKey="record => record.Id">
        <a-table-column key="DpiaViewName" data-index="DpiaViewName">
          <span slot="title" class="min-w-90p">{{ $t('user.verification-code.required1115661105714') }}</span>
        </a-table-column>
        <a-table-column key="DpiaViewCreatetime" data-index="DpiaViewCreatetime">
          <span slot="title" class="min-w-90p">{{ $t('user.verification-code.required111566131') }}</span>
        </a-table-column>
        <a-table-column key="action" :title="$t('app.setting.othersettings')">
          <template slot-scope="text, record">
        <span>
           <!-- 删除按钮 -->
           <a-popconfirm :title="$t('user.verification-code.required111566110577225')"
                         :ok-text="$t('user.verification-code.required1166')" :cancel-text="$t('user.email.required11')"
                         @confirm="() => del(record.Id)">
              <a-button type="link" size="small">{{ $t('app.setting.copy') }}</a-button>
            </a-popconfirm>
        </span>
          </template>
        </a-table-column>
      </a-table>
    </a-drawer>
    <PermissionsDrow ref="PermissionsDrow" :types="state" :dpiaId='dpiaId' @succeed="getList"></PermissionsDrow>
  </div>

</template>

<script>
import PermissionsDrow from '../components/PermissionsDrow.vue'
import {delpermissions, delWrites, permissionsList, writesList} from '@/api/dpia/form'

const columns = [
  {
    title: '姓名',
    className: 'min-w-90p',
    dataIndex: 'DpiaViewName',
    align: 'center',
  },
  {
    title: '创建时间',
    className: 'min-w-90p',
    dataIndex: 'DpiaViewCreatetime',
    align: 'center',
  },
  {
    title: '操作',
    width: '150px',
    dataIndex: '',
    align: 'center',
    scopedSlots: {customRender: 'action'},
  },
]
export default {
  components: {
    PermissionsDrow
  },
  data() {
    return {
      columns: columns,
      /**table数据 */
      listData: [],
      // table的loading 状态
      listLoading: false,
      // 下面这些都是通用的
      form: this.$form.createForm(this),
      visible: false,
      loading: false,
      // 分页参数
      pagination: {
        current: 1, //当前页
        pageSize: 10, //每页条数
        total: 0, //总条数
        showQuickJumper: true,
        hideOnSinglePage: true,
        showTotal: (total) => this.$t('user.verification-code.required111566110577216', {total}, 'user.verification-code.required111566110577217'),
      },
      dpiaId: null,
      //区别多人答题还是功能权限
      state: ''
    }
  },
  methods: {

    async open(data = {}, state) {
      //区别多人答题还是功能权限
      this.state = state
      this.dpiaId = null
      this.dpiaId = data.Id
      this.visible = true
      this.pagination.current = 1
      this.getList()
    },

    async getList() {
      this.listLoading = true
      const {current, pageSize} = this.pagination
      try {
        if (this.state == 'write') {
          // 调用查询列表
          const {data} = await writesList({
            ...{page: current, pageSize},
            Id: this.dpiaId,
          })
          // 解构赋值
          const {Page, PageSize, Total, Lists} = data
          console.log(Lists);
          const arr = []
          Lists.forEach(item => {
            arr.push({
              DpiaViewName: item.DpiaWriteName,
              DpiaViewCreatetime: item.DpiaWriteCreatetime,
              Id: item.Id
            })
          })
          // 给table赋值
          this.listData = arr || []
          // 给分页数据赋值
          this.pagination = {
            current: Page,
            pageSize: PageSize,
            total: Total,
          }
        } else {
          // 调用查询列表
          const {data} = await permissionsList({
            ...{page: current, pageSize},
            Id: this.dpiaId,
          })
          // 解构赋值
          const {Page, PageSize, Total, Lists} = data
          // 给table赋值
          this.listData = Lists || []
          // 给分页数据赋值
          this.pagination = {
            current: Page,
            pageSize: PageSize,
            total: Total,
          }

        }


      } catch (error) {
        // 报错信息
        this.$message.error(error)
      } finally {
        // 不管成功失败都会执行
        this.listLoading = false
      }
    },

    // 分页事件 直接复制使用 通用的
    tableChange(pagination) {
      this.pagination = pagination
      this.getList()
    },

    create() {
      this.$refs.PermissionsDrow.open(this.state)
    },

    async del(id) {
      try {
        if (this.state == 'write') {
          // 传过来的id直接请求删除api
          await delWrites({Ids: id})
        } else {
          // 传过来的id直接请求删除api
          await delpermissions({Ids: id})
        }
        this.$message.success(this.$t('user.verification-code.required111566110577133'))
        // 重新初始化列表刷新数据
        this.getList()
      } catch (error) {
        this.$message.error(error)
      } finally {
      }
    },
    // 关闭 通用的 
    close() {
      // 修改侧边栏状态
      this.visible = false
      // 清空form的值
      this.form.resetFields()
    },
  }
}
</script>

<style>

</style>